import { Component, ViewChild, AfterViewInit, OnInit } from "@angular/core";
import { PromotionService } from './../../vsp/bean';
import * as _ from 'lodash';
import { Router, ParamMap } from '@angular/router';


@Component({
  selector: 'promotion',

  styleUrls: ['./promotion.component.css'],
  templateUrl: './promotion.component.html',
  providers: [PromotionService]
})
export class PromotionComponent implements OnInit {
  public page: any;
  public limit: any;
  public totalCount: any;
  public isend: boolean = false;
  public promotionList: Array<any> = [];
  public loading: boolean = true;
  constructor(
    private promotionService: PromotionService,
    private router: Router

  ) {
  }


  ngOnInit() {
    
    this.page = 1;
    this.limit = 10;
    this.promotionService.getPromotion(this.page, this.limit).then(res => {
      this.promotionList = res.json().page.list;
      _.map(this.promotionList, (item) => {
        if (item.goodsPrice && item.couponPrice) {
          item.finalPrice = item.goodsPrice - item.couponPrice;

        } else {
          return;
        }
      });
      this.totalCount = res.json().page.totalCount;
    })
   
  }
  public showNextPage() {
    this.page++;
    if (!this.isend) {
      this.promotionService.getPromotion(this.page, this.limit).then(res => {
        if (res.json().page.totalPage == res.json().page.currPage) {
          this.isend = true;
          this.loading = false;
        }
        this.promotionList = this.promotionList.concat(res.json().page.list);
        _.map(this.promotionList, (item) => {
          if (item.goodsPrice && item.couponPrice) {
            item.finalPrice = item.goodsPrice - item.couponPrice;

          } else {
            return;
          }
        });
        this.totalCount = res.json().page.totalCount;
      })
      this.loading = true;
    }
  }
  public jumpdetail(id){
    this.router.navigate(['/good', id]);
    
  }

}
